package com.zb.frame.base.upgrade.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBHelper {

    private static Logger logger = LoggerFactory.getLogger(DBHelper.class);

    public static void main(String[] args) {
        testGetRealDatabase();
    }

    public static void testGetRealDatabase(){
        String url1 = "jdbc:db2://127.0.0.1:5000/testdb";
        System.out.println(getRealDatabase("DB2",url1));
        System.out.println(getDbType(url1));
        //String url2 = "jdbc:oracle:thin:@127.0.0.1:1521:testdb";
        String url2 = "jdbc:oracle:thin:@127.0.0.1:1521/testdb.test";
        System.out.println(getRealDatabase("ORACLE",url2));
        System.out.println(getDbType(url2));
        String url3 = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=testdb";
        System.out.println(getRealDatabase("SQLSERVER",url3));
        System.out.println(getDbType(url3));
        String url4 = "jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=UTF-8";
        System.out.println(getRealDatabase("MYSQL",url4));
        System.out.println(getDbType(url4));
        String url5 = "jdbc:postgresql://127.0.0.1:5432/testdb";
        System.out.println(getRealDatabase("POSTGRESQL",url5));
        System.out.println(getDbType(url5));
        String url6 = "jdbc:hive://127.0.0.1:10000/testdb";
        System.out.println(getRealDatabase("HIVE",url6));
        System.out.println(getDbType(url6));
        String url7 = "jdbc:hive2://30.4.184.129:10000/gdb_all_sx_safe";
        System.out.println(getRealDatabase("HIVE",url7));
        System.out.println(getDbType(url7));
    }

    public static String getDbType(String url){
        String[] strs = url.split(":");
        String dbType = strs[1];
        if("jtds".equals(dbType))
            dbType = strs[2];
        return dbType;
    }

    private static String getRealDatabase(String dbType , String url){
        String db = "";
        String[] arr;
        switch(dbType.toUpperCase()){
            case "DB2":
            case "DB2_V9":
                //默认端口5000
                //jdbc:db2://<machine_name>:<port>/<dbname>
                //jdbc:db2://<machine_name>/<dbname>
                db = url.substring(url.lastIndexOf("/")+1);
                break;
            case "ORACLE":
                //默认端口1521
                //jdbc:oracle:thin:@<machine_name>:<port>:<dbname>
                //jdbc:oracle:thin:@<machine_name>:<port>/<servicename>
                arr = url.split("@");
                String[] vls = arr[1].split(":");
                if(url.toUpperCase().indexOf("@(DESCRIPTION=") != -1){

                } else if(vls.length == 2){
                    db = vls[1].substring(vls[1].indexOf("/") + 1);
				/*if(db.split("\\.").length>0){
					db = db.split("\\.")[0];
				}*/
                } else {
                    db = vls[2];
                }
                break;
            case "SQLSERVER":
            case "MSSQL":
                //默认端口1433
                //jdbc:jtds:sqlserver://<machine_name>:<port>;DatabaseName=<dbname>
                //jdbc:sqlserver://<machine_name>:<port>;DatabaseName=<dbname>
                arr = url.substring("jdbc:sqlserver://".length()).split(":");
                db = arr[1].substring(arr[1].indexOf("=") + 1);
                break;
            case "MYSQL":
                //默认端口3306
                //jdbc:mysql://<machine_name>:<port>/<dbname>
                //jdbc:mysql://<machine_name>:<port>/<dbname>?useUnicode=true&characterEncoding=UTF-8
                if(url.indexOf("?") == -1){
                    arr = url.substring("jdbc:mysql://".length()).split(":");
                }else{
                    arr = url.substring(0,url.indexOf("?")).substring("jdbc:mysql://".length()).split(":");
                }
                db = arr[1].substring(arr[1].indexOf("/") + 1);
                break;
            case "POSTGRESQL":
                //默认端口5432
                //jdbc:postgresql://<machine_name>:<port>/<dbname>
                arr = url.substring("jdbc:postgresql://".length()).split(":");
                db = arr[1].substring(arr[1].indexOf("/") + 1);
                break;
            case "HIVE":
                //默认端口10000
                //jdbc:hive://<machine_name>:<port>/<dbname>
                db = url.substring(url.lastIndexOf("/")+1);
                break;
        }
        return db;
    }

    public Connection getConnectionJDBC(String dbDriver, String username, String password, String dbUrl) {
        Connection conn = null;
        try {
            Class.forName(dbDriver);
            Properties prop = new Properties();
            prop.setProperty("user", username);
            prop.setProperty("password", null == password? "" : password);
            conn = DriverManager.getConnection(dbUrl, prop);
        } catch (ClassNotFoundException e) {
            logger.error("JDBC Driver is not find:",e);
        } catch (SQLException e) {
            logger.error("JDBC connected is wrong:",e);
        }
        return conn;
    }
}
